package com.usian.search.listener;

import com.usian.common.util.JsonUtils;
import com.usian.search.service.ApArticleSearchService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

import static com.usian.common.constants.message.WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC;

/**
 * 文章上下架同步索引库
 */
@Component
public class ArticleUpDownListener {

    @Autowired
    private ApArticleSearchService apArticleSearchService;

    @KafkaListener(topics = WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<?,?> record){
        //判断消息内容是否存在
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            //获取消息内容
            String value = (String) record.value();
            Map<String, Integer> map = JsonUtils.toMap(value, String.class, Integer.class);
            //同步到索引库  1上架 0下架
            apArticleSearchService.ArticleUpDown(map.get("wmNewsId"),map.get("enable"));
        }
    }
}
